package com.ay.weixin.fdc.front.invite.event;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import org.lizar.config.keys.Keys;
import org.lizar.controller.Event;
import org.lizar.controller.intercepter.Before;
import org.lizar.controller.loader.BasicLoader;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.json.JList;
import org.lizar.json.JObject;
import org.lizar.util.StringHelper;

import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.fdc.admin.category.domain.CategoryLoader;
import com.ay.weixin.fdc.boss.bank.db.BankAccountDao;
import com.ay.weixin.fdc.boss.user.db.UserDao;
import com.ay.weixin.fdc.boss.user.domain.UserBasic;
import com.ay.weixin.fdc.boss.yongjin.db.IbatisCashFlow;
import com.ay.weixin.fdc.boss.yongjin.db.IbatisCashWithdrawFlowDao;
import com.ay.weixin.fdc.boss.yongjin.domain.CashFlow;

public class MyInvitorsEvt extends Event implements Before{
	private static IbatisCashFlow cash_dao;
	private UserDao user_dao;
	
	@Override
	public void before(BasicLoader bl) throws ServletException, IOException {
	}
	
	@Override
	public void init_property() throws Exception {
		cash_dao=Container.get(IbatisCashFlow.class);
		user_dao=Container.get(UserDao.class);
		
	
	}

	@Override
	public String default_handling_path() {
		return "/myinvitors/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		long wxproject_id=el.queryPathInt(1);
		
		if(!el.getBool("is_weixin")&&ProjectConfig._bool(wxproject_id,"sys.weixin_only")){
			el.text("invalid visit");
			return;
		}
		if(el.getSessionAttr("user")==null){
			el.response_to_root("/fdc/login/"+wxproject_id);
			return;
		}
		if(el.queryPathCheck(2,"")){//查看我要邀请的朋友
			view(el,wxproject_id);
		}else if(el.queryPathCheck(2,"next")){
			view_others_next(el);
		}else if(el.queryPathCheck(2,"code")){
			code(el,wxproject_id);
		}
	}
	
	private void code(EventLoader el,long project_id) throws IOException, ServletException{
		el.template("/WEB-INF/template/app/"+project_id+"/invite/code.vm");
	}
	
	private void view_others_next(EventLoader el) throws IOException, ServletException{
		long user_id=el.getLong("user_id",-1);
		long from_id=el.getLong("from_id",-1);
		JObject obj=new JObject();
		if(from_id==-1){
			obj.put("err", "lack of args");
			obj.put("data",new JList());
			el.json(obj.toString());
			return;
		}
		if(user_id==-1){
			obj.put("err", "lack of args");
			obj.put("data",new JList());
			el.json(obj.toString());
			return;
		}
		UserBasic user=null;
		try {
			user=user_dao.getById(user_id);
		} catch (SQLException e1) {
			e1.printStackTrace();
			obj.put("err", el.i18("fdc_cash_withdraw_server_error"));
			obj.put("data",new JList());
			el.json(obj.toString());
			return;
		}
		if(user==null){
			obj.put("err", "user not exists");
			obj.put("data",new JList());
			el.json(obj.toString());
			return;
		}
		
		UserBasic me=el.getSessionAttr("user",UserBasic.class);
		
		if(!StringHelper.equals(me.getInvite_code(), user.getFrom_invite_code())){
			obj.put("err", "user not matched");
			obj.put("data",new JList());
			el.json(obj.toString());
			return;
		}
		
		
		List<CashFlow> list=null;
		
		try {
			list=cash_dao.getTotalFlowNextPage(user.getId(), from_id);
		} catch (SQLException e) {
			e.printStackTrace();
			obj.put("err", "server error");
			obj.put("data",new JList());
			el.json(obj.toString());
			return;
		}
		
		JList jlist=JList.toJson(list);
		obj.put("data",jlist);
		el.json(obj.toString());
	}
	
	public void view(EventLoader el,long project_id) throws IOException, ServletException{
		UserBasic user=el.getSessionAttr("user",UserBasic.class);
		
		List<UserBasic> ulist=null;
		
		try {
			ulist=user_dao.getMyInviteListFirstPage(user.getInvite_code());
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("db.failure"));
		}
		
		el.setAttr("ulist",ulist);
		
		el.template("/WEB-INF/template/app/"+project_id+"/invite/index.vm");
	}
	
	
}
